<?php
/**
 * Backend Image Thumbnail Generator
 *
 * Version 1.0
 *
 * A library capable of creating thumbnail version of a given image when
 * provided the expected output width and height. The script handles out of
 * proportion thumbnail scenarios gracefully, user is able to specify which
 * part of the original image should be cropped out in such cases. The script
 * is capable of either outputting thumbnail to browser or save in to the
 * file system, can be used an effective backend tool for image uploading
 * applications.
 *
 * --------------------------------------------------------------------------
 *
 * @category    Image Manipulation
 * @author      Subhash Vithanapathirana <subhash@monvia.com>
 * --------------------------------------------------------------------------
 */

class ThumbnailGen {
    public static function resizeImage($fileName, $newWidth, $newHeight = null, $cropType = 0, $outputFileName = "", $quality = 75){
        $imgCreateFun=array(1 => "imagecreatefromgif", 2 => "imagecreatefromjpeg", 3 => "imagecreatefrompng");
        $imgOutputFun=array(1 => "imagegif", 2 => "imagejpeg", 3 => "imagepng");

        if(file_exists($fileName)){
            $myFileInfo = getimagesize($fileName);
            $imgWidth = $myFileInfo[0];
            $imgHeight = $myFileInfo[1];

            if ($newHeight === null) {
              $divFactor = $imgWidth/$newWidth;
              if ($divFactor < 1) {
                $newWidth = $imgWidth;
                $newHeight = $imgHeight;
              } else {
                $newHeight = $imgHeight/$divFactor;
              }
            }
            
            $resCords = self::getPropSizes($imgWidth, $imgHeight, $newWidth, $newHeight, $cropType);

            $imgType = $myFileInfo[2];
            if(in_array($imgType,array_keys($imgCreateFun))){
                $image_p = imagecreatetruecolor($newWidth, $newHeight);
                $image = $imgCreateFun[$imgType]($fileName);
                imagecopyresampled($image_p, $image, 0, 0, $resCords["srcX"], $resCords["srcY"], $newWidth, $newHeight, $resCords["srcW"], $resCords["srcH"]);
                if(!file_exists($outputFileName)){
                    if(!$outputFileName){
                        header("Content-type: ".$myFileInfo["mime"]);
                    }
                    $imgOutputFun[$imgType]($image_p, $outputFileName, $quality);
                    imagedestroy($image_p);
                    imagedestroy($image);
                }
                else{
                    imagedestroy($image_p);
                    imagedestroy($image);
                    die("Cannot write output image - Filename already exists");
                }

            }
            else{
                die("Image Type not supported");
            }
        }
        else{
            die("Source file not found");
        }
    }

    private static function getPropSizes($orgWidth, $orgHeight, $setWidth, $setHeight, $cropType = 0){
        $divXFac = $orgWidth / $setWidth;
        $divYFac = $orgHeight / $setHeight;
        $divFac = ($divXFac <= $divYFac)?$divXFac:$divYFac;
        $newWidth = $orgWidth / $divFac;
        $newHeight = $orgHeight / $divFac;
        $orgWidthDef = ($newWidth - $setWidth) * $divFac;
        $orgHeightDef = ($newHeight - $setHeight) * $divFac;
        if($cropType == -1){
            $retArr["srcX"] = 0;
            $retArr["srcY"] = 0;
        }
        elseif($cropType == 0){
            $retArr["srcX"] = $orgWidthDef/2;
            $retArr["srcY"] = $orgHeightDef/2;
        }
        elseif($cropType == 1){
            $retArr["srcX"] = $orgWidthDef;
            $retArr["srcY"] = $orgHeightDef;
        }
        $retArr["srcW"] = $orgWidth - $orgWidthDef;
        $retArr["srcH"] = $orgHeight - $orgHeightDef;
        return $retArr;
    }
}
?>
